﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data.OleDb;

namespace ApplicationBll
{
    public class Card
    {
        private AccessSQL asql = new AccessSQL();

        public string GetJson(string nodeID)
        {
            string id = nodeID.Substring(13);
            List<TreeNode> list = GetListByCardParentID(id);
            return JsonConvert.SerializeObject(list);
        }

        public int AddCardInfo(string name,int pid)
        {
            string sql = "INSERT INTO Card (CardName, CardParentID) VALUES(@name,@pid)";
            return asql.ExecuteNonQuery(sql, new OleDbParameter("@name", name), new OleDbParameter("@pid", pid));
        }
        
        private List<TreeNode> GetListByCardParentID(string id)
        {
            string sql = string.Format("SELECT ID, CardName, CardTip, CardParentID FROM Card WHERE (CardParentID = {0})", id);
            DataTable table = asql.GetDataTable(sql);
            List<TreeNode> result = new List<TreeNode>();
            foreach (DataRow row in table.Rows)
            {
                TreeNode node = new TreeNode();

                node.id = "card_node_id_" + row["ID"];
                node.text = row["CardName"].ToString();
                node.leaf = row["CardParentID"].ToString() != "0";//卡树分两层，父节点ID不等于0的为叶节点
                result.Add(node);

            }
            return result;
        }


    }
}
